/*****************************************************************************/
/** \file function.h
**
** 
**
** History:
** 
*****************************************************************************/
#ifndef  __FUNCTION_H__
#define  __FUNCTION_H__

/*****************************************************************************/
/* Include files */
/*****************************************************************************/

/*****************************************************************************/
/* Global pre-processor symbols/macros ('#define') */
/*****************************************************************************/
#define  s8   signed char
#define  s16  signed short int
#define	 s32  signed int 
#define	 u8   unsigned char 
#define	 u16  unsigned short int 
#define	 u32  unsigned int 

#define Fault_Flag		    			u_Fault_Flag.Byte
#define	Fault_ShortCircuit				u_Fault_Flag.Bits.Bit0
#define	Fault_UnderVoltage				u_Fault_Flag.Bits.Bit7	
#define	Fault_StartFail					u_Fault_Flag.Bits.Bit6 
#define	Fault_OverVoltage				u_Fault_Flag.Bits.Bit5 
#define	Fault_TorgueLoss				u_Fault_Flag.Bits.Bit4 
#define	Fault_OverCurrent				u_Fault_Flag.Bits.Bit3 
#define	Fault_PhaseLoss					u_Fault_Flag.Bits.Bit2 
#define	Fault_Block						u_Fault_Flag.Bits.Bit1

#define Brk_Flag			u_Brk_Flag.Byte
#define Fault_Brake			u_Brk_Flag.Bits.Bit0
#define Fault_Braking		u_Brk_Flag.Bits.Bit1


/*****************************************************************************/
/* Global type definitions ('typedef') */
/*****************************************************************************/
typedef enum 
{
	SYS_INIT	= 0,
	SYS_RESTART	= 1,
	SYS_RUN		= 2,
	SYS_FAULT	= 3,
	SYS_STOP	= 4,
	SYS_WAIT	= 5
}SystStates;

typedef enum 
{
	MC_INIT		= 0, 
	MC_CHARGE	= 1, 
	MC_REVWIND	= 2, 
	MC_START	= 3, 
	MC_SW		= 4, 
	MC_RUN		= 5,   
	MC_STOP		= 6, 
	MC_BRAKE	= 7
}MotorStates;

typedef struct
{
	s32			IP_A;
	s32			IP_B;
	s32			IP_C;
	s32 		Vbus;
	s32			Ibus;
	s32			VCtr;
	s32			VTem;
	s32			Power;	
	s32			dq_power;	
	s32			Speed_Close;	
	s32			Speed_Min;
	s32			Speed_Base;
	s32			Speed_Stop;	
	s32			Calb_Speed;
	s32			Calb_SpeedLimit;
	s32			Calb_Vbus;
	s32			Mech_Speed;
	s32			FG_Speed;
	s32 		We;
	s32			DeltaTheta;	
	s32			Freq_EPWM;
	s32 		Kslf_L;
	s32 		Kslf_H;
	s32 		Tpwm_us;
	s32 		CF_we;
	s32 		We_Ke;	
	s32			Speed_Ke;
	u8			KBW_H;
	float		MotorVolt;
	float		MotorCurr;
	float		R_Shunt;	
	float		Gain_AMP;	
	float		REF_ADC;	
	float		REF_AMP;	
	float		Ke;
	float		BEMFVolt;	
	float		Kslf_bemf;
	
}Basic_Components;

typedef struct
{
	s32			Speed_Ref; 
	s32			Curr_Is_Ref; 
	s32			Curr_Iq_Max;
	s32			Curr_Iq_Min;
	s32 		Elec_Angle;
	u32 		MotorRunTime;	
	s32 		Is_beta;
	s32 		IPD_Angle;
	u16 		W_Cycle;
	u16 		I_Cycle;	
	u16 		WRAMP;
	u16 		ERAMP;
	u16 		IRAMP;
	u16 		FRAMP;
	s32 		Vs_Max;
	s32 		Vs_Square_Max;
	u16 		EPWM_Period;
	u16 		EPWM_SAMPDLY;
	u16 		TSAM;
	s32			Kslf;
	s32			Weakening;
	s32			Vout;
	s32			PowerLimit;
}FOC_Components;

typedef struct
{
	u8  		IP_CH;
	s32 		ADVal[7];
	s32 		ARR[7];
	s32 		REF[7];
	s32			G_Ip; 
	s32			G_Ib; 
	s32			G_Vb; 
	s32			G_Vc; 
	s32			G_Sp; 
	s32			G_Vt;	
	s32			G_Pw;
}ADT_Components;

typedef struct
{
	u8			MC_RunStop:1;
	u8			MotorRuing:1;
	u8			FG_Out:1;
	u8			FG_D180:1;
	u8			MotorFR;
	u8			MotorFR_Pre;
	u8     		KeyOnOff;
	u8			Charge;
	u8			IPD;
	u8			Obse_Vbus;
}Flag_Components;

typedef struct
{
	u8	 		Sample_Chenal;
	u16  		Charge_Count;
	u16	 		Motor_Restart;
	u16			FG_Count;
	u32	 		Motor_Stophold;
	u32	 		Motor_StartFail;	
	u32	 		Motor_Brake;
}Time_Components;

typedef struct
{
	u16  		Charge_Count;
	u32	 		Motor_Restart;
	u32  		IPDOL_Count;
	u16  		TimeBase[5];

}Count_Components;

typedef struct
{
	u8			Block_Cnt;
	u8			PhaseLoss_Cnt;
	u8			StartFail_Cnt;
	u8			Brake_Cnt;
	u8			TorgueLoss_Cnt;
	u16			PhaseA_LossCnt;
	u16			PhaseB_LossCnt;
	u16			PhaseC_LossCnt;
	u16			Phaseloss_Value;
	u32			OverVoltage_Value;
	u32			LowerVoltage_Value;
	u32			OV_Recover_Value;
	u32			UV_Recover_Value;
	s32			OverCurrent_Value;
}Fault_Components;

typedef	union
{
	unsigned char Byte;
	struct
	{
		unsigned char	Bit0	:1;
		unsigned char	Bit1	:1;
		unsigned char	Bit2	:1;
		unsigned char	Bit3	:1;
		unsigned char	Bit4	:1;
		unsigned char	Bit5	:1;
		unsigned char	Bit6	:1;
		unsigned char	Bit7	:1;
	}Bits;
}Union_Fault;

/*****************************************************************************/
/* Global variable declarations ('extern', definition in C source) */
/*****************************************************************************/
extern volatile Union_Fault				u_Fault_Flag;
extern volatile Union_Fault				u_Brk_Flag;
extern volatile SystStates 				SYSTEM_STATE;
extern volatile MotorStates 			MOTOR_STATE;

extern Basic_Components					Basic;
extern ADT_Components 					Samp;
extern FOC_Components					FOC;
extern Flag_Components 					Flag;
extern Time_Components    				Time; 
extern Count_Components   				Count; 
extern Fault_Components 				Fault;

/*****************************************************************************/
/* Global function prototypes ('extern', definition in C source) */
/*****************************************************************************/
extern void 							MC_Control(void);
extern void 							Key_Control(void);
extern void 							MC_Check_MotorFault(void);
extern void 							MC_Calc_MotorSpeed(void);
extern void  							MC_Speed_Control(void);
extern void 							Clear_Fault_Message(void);
extern void 							SysTick_1ms(void);
extern void 							UartView(s16 view1,s16 view2,s16 view3,s16 view4);
extern void 							u_Motor_On(void);
extern void 							u_Motor_Off(void);
extern void 							u_Motor_FRControl(u8 Input_Val);
extern void 							u_Motor_Speed_Set(s32 Input_Val);
extern void 							Sys_Wakeup_task(void);
extern void 							System_Modules_off(void);

#endif	 /* __FUNCTION_H__ */


